<!--
PAGE DOCUMENTATION:
==================
This is the application's home page. 
Shows the user how many new messages he has.

-->

<html>
<?php
	echo "
	<fb:dashboard>
	<fb:action href=\"invite.php\">
	Invite some friends over!
	</fb:action>
	<fb:action href=\"sendMessage.php\">
	Send a voice message to a friend!
	</fb:action>
	<fb:action href=\"getMessages.php\">
	Hear a voice message from your friends!
	</fb:action>
	<fb:help href=\"help.php\">
	Help Me!
	</fb:help>
	</fb:dashboard>";

	require_once 'design.php';
	require_once 'sqlLib.php';
	require_once 'logsLib.php';
 	require_once 'appinclude.php';
 	require_once 'generalLib.php';    
	$currentPage ='index';	
	
	//show welcome message $ image:
	echo "<h1 align=\"center\" class=\"welcomeMessage\">Welcome to</h1>
		<div align=\"center\">
		<img src=\"$server/grafics/logo.JPG\" >
		</div>
		<div align=\"center\">
		<h3 class=\"welcomeMessage\">(The best Facebook application, like, ever!!)</h3>
		</div>
		<div align=\"center\">&nbsp;</div>
		<div align=\"center\">&nbsp;</div>";
	
		
	// get user ID:
	$user_id = $facebook->user;
	// create commection to DB:
	$sqlDb = new mysqli("localhost", "root", "", "messages");
	/* check connection */
	if (mysqli_connect_errno()) {
		documentError("Connect failed: ".mysqli_connect_error());
		printErrorMsg("connecting to the database");
	} else {
		logMysqliConnectionOpened($user_id,$currentPage);
		// get number of new messages for user:
		$numberOfNewMessages = countMessages($sqlDb,$user_id,"unread");
		logFunctionCall($user_id, 'countMessages', 'sqlDb,'.$user_id.',unread', $numberOfNewMessages,$currentPage);
		//// get number of messages sent from user:
		$numberOfSentMessages = countMessages($sqlDb,$user_id,"sent");
		logFunctionCall($user_id, 'countMessages', 'sqlDb,'.$user_id.',sent', $numberOfSentMessages,$currentPage);
		//close connection to DB:	
		$sqlDb->close();
		logMysqliConnectionClosed($user_id,$currentPage);
		
		//show how many new messages for user and update user's profile box:
		if ($numberOfNewMessages != -1) {
			if ($numberOfNewMessages==1) {
				$pronoun = "it"	;
			} else {
				$pronoun = "them";
			}
			
			echo "<div align=\"center\">
					<h2 class=\"messagesStatusLine\">
					Hi <fb:name uid=\"$user_id\" firstnameonly=\"true\" useyou=\"false\" />,</br>";
			if($numberOfNewMessages == 0){
				echo "You have no new messages.";
			} elseif ($numberOfNewMessages == 1) {
				echo "You have one new message.";
			} else {
				echo "You have $numberOfNewMessages new messages.";
			}
			echo "</h2>
					</div>
					<div align=\"center\">&nbsp;</div>
					<div align=\"center\" class=\"comment\">";
			if($numberOfNewMessages == 0){
				echo "Be the better friend,</br>";
			} elseif($numberOfNewMessages == 1){
				echo "(You can do better....)";
			} else {
				echo "(OMG you're so popular...)";
			}				
			echo "</div>
					<div align=\"center\">
					&nbsp;
					</div>
					<div align=\"center\" class=\"options\">
					<b>
					</br>";
			if ($numberOfNewMessages != 0) {
				echo "<a href=\"getMessages.php\">Listen to $pronoun</a> or ";
			}
			echo "<a href=\"sendMessage.php\">Send a message to one of your friends</a>.
					</b>
					</div>
					<div align=\"center\">
					&nbsp;
					</div>";
		}
	}

	profileBox($user_id, $numberOfNewMessages, $numberOfSentMessages,$facebook);	
		
	echo "<p class=\"options_bottom\" style=\"margin-left: 10px\">Not getting enough messages? <a href=\"invite.php\">
			Invite some of your friends over</a>.</p>
			<p class=\"options_bottom\" style=\"margin-left: 10px\">Lost? <a href=\"help.php\">Go to the help page</a>.</p>
			<p class=\"options_bottom\" style=\"margin-left: 10px\"><b>Looking for love?</b> You're using the wrong 
			application.</p>
			<img src=\"$server/tin_can_phone2.jpg\" >";

?>
</br>
<font size="3">
This application participates in Unicell's contest. <a href="http://www.liveface.co.il/%D7%AA%D7%97%D7%A8%D7%95%D7%AA-%D7%90%D7%A4%D7%9C%D7%99%D7%A7%D7%A6%D7%99%D7%95%D7%AA-%D7%A4%D7%99%D7%99%D7%A1%D7%91%D7%95%D7%A7/apps/" > For rating and other applications. </a>
</font>
</html>